package com.forlgb.powercloud.base.client.source.service.impl;

import com.forlgb.powercloud.base.client.source.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements RedisService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void set(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    @Override
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    @Override
    public boolean expire(String key, long expire) {
        return stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }

    @Override
    public void remove(String key) {
        stringRedisTemplate.delete(key);
    }

    @Override
    public Long increment(String key, long delta) {
        return stringRedisTemplate.opsForValue().increment(key, delta);
    }

    @Override
    public void setList(String key, List list) {
        redisTemplate.opsForList().leftPushAll(key, list);
    }

    @Override
    public List getList(String key) {
        return redisTemplate.opsForList().range(key, 0, -1);
    }

    @Override
    public void setMap(String key, Map map) {
        redisTemplate.opsForHash().putAll(key, map);
    }

    @Override
    public Map getMap(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    @Override
    public void setHashMap(String key, HashMap hashMap) {
        redisTemplate.opsForHash().putAll(key, hashMap);
    }

    @Override
    public HashMap getHashMap(String key) {
        return (HashMap) redisTemplate.opsForHash().entries(key);
    }
}
